<?php

declare (strict_types = 1);

namespace app\api\controller;

use app\api\validate\UserValidate;
use app\common\service\file\FileUploadsService;
use app\common\service\system\ProvinceService;
use app\common\service\system\RegionService;
use app\common\service\user\UserService;
use app\common\service\VersionService;
use app\common\util\ApiResponseTrait;
use think\Request;

class Common {

    use ApiResponseTrait;

    protected Request $request;
    
    /**
     * 构造函数、依赖注入
     *
     * @param Request $request
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * 获取登录挑战（nonce）
     *
     * @param UserService $service
     * @return \think\Response
     * @throws \Random\RandomException
     */
    public function getNonce(UserService $service): \think\Response
    {
        $address = $this->request->get('address', '');
        return $this->success($service->getNonce($address));
    }

    /**
     * bsc登录
     *
     * @param UserService $service
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function bscLogin(UserService $service): \think\Response
    {
        $data = $this->request->post();
        return $this->success($service->bscLogin($data));
    }

    /**
     * 退出登录
     *
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function logout(): \think\Response
    {
        $user_id = $this->request->userInfo['user_id'];
        $token = $this->request->userInfo['token'];
        $service = app()->make(UserService::class);
        return $this->success($service->logout($user_id, $token));
    }

//    /**
//     * 上传
//     *
//     * @return \think\Response
//     * @throws \Exception
//     */
//    public function upload(): \think\Response
//    {
//        $fileService = app()->make(FileUploadsService::class);
//        return $this->success($fileService->upload());
//    }
//
//    /**
//     * 下载
//     *
//     * @return \think\response\File|\think\Response
//     */
//    public function download(): \think\response\File|\think\Response
//    {
//        $filename = $this->request->get('filename');
//        $type = $this->request->get('type');
//        // 文件存储的实际路径，这里以public目录下的uploads为例
//        $filePath = public_path() . "storage/{$type}/" . $filename;
//
//        // 检查文件是否存在
//        if (!file_exists($filePath)) {
//            return $this->notValidate('文件不存在');
//        }
//
//        // 返回文件下载响应
//        return download($filePath);
//    }
}